package com.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {


    /**
     * 生成32位md5码
     * @param s
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String encode(String s) throws NoSuchAlgorithmException {
        //获取一个信息摘要器
        MessageDigest digest = MessageDigest.getInstance("md5");
        byte[] bytes = digest.digest(s.getBytes());
        StringBuffer buffer = new StringBuffer();
        for(byte b: bytes) {
            //通过与运算获取字符串
            String str = Integer.toHexString(b & 0xff);
            if(str.length() == 1) {
                buffer.append("0");
            }
            buffer.append(str);
        }
        //返回结果
        return buffer.toString().toUpperCase();
    }

}
